<template>
  <page-header-wrapper :title="false">
    <a-card title="采购单审核">
      <s-table
        bordered
        ref="table"
        size="middle"
        :row-key="(record) => record.id"
        :columns="purchaseColumns"
        :data="purchaseData"
      >
        <div
          slot="filterDropdown"
          slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
          style="padding: 8px"
        >
          <a-input
            v-ant-ref="c => (searchInput = c)"
            :placeholder="`搜索${column.title}`"
            :value="selectedKeys[0]"
            style="width: 188px; margin-bottom: 8px; display: block;"
            @change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
            @pressEnter="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
          />
          <a-button
            type="primary"
            icon="search"
            size="small"
            style="width: 90px; margin-right: 8px"
            @click="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
          >
            搜索
          </a-button>
          <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
            重置
          </a-button>
        </div>
        <div
          slot="filterAdviser"
          slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
          style="padding: 8px"
        >
          <a-select
            show-search
            :filter-option="filterOption"
            :placeholder="`选择顾问`"
            style="width: 188px; margin-bottom: 8px; display: block;"
            :value="selectedKeys[0]"
            @change="(value) => setSelectedKeys(value ? [value] : [])"
            @pressEnter="() => handleSelect1(selectedKeys, confirm, column.dataIndex)"
          >
            <a-select-option v-for="(item, index) in adviserOpts" :value="index" :key="index">{{item}}</a-select-option>
          </a-select>
          <a-button
            type="primary"
            icon="search"
            size="small"
            style="width: 90px; margin-right: 8px"
            @click="() => handleSelect1(selectedKeys, confirm, column.dataIndex)"
          >
            搜索
          </a-button>
          <a-button size="small" style="width: 90px" @click="() => handleReset1(clearFilters, column.dataIndex)">
            重置
          </a-button>
        </div>
        <div
          slot="filterCategory"
          slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
          style="padding: 8px"
        >
          <a-select
            :placeholder="`选择${column.title}`"
            style="width: 188px; margin-bottom: 8px; display: block;"
            :value="selectedKeys[0]"
            @change="(value) => setSelectedKeys(value ? [value] : [])"
            @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
          >
            <a-select-option v-for="(item, index) in categoryOpts" :value="index" :key="index">{{item}}</a-select-option>
          </a-select>
          <a-button
            type="primary"
            icon="search"
            size="small"
            style="width: 90px; margin-right: 8px"
            @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
          >
            搜索
          </a-button>
          <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
            重置
          </a-button>
        </div>
        <div
          slot="filterPayment"
          slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
          style="padding: 8px"
        >
          <a-select
            :placeholder="`选择${column.title}`"
            style="width: 188px; margin-bottom: 8px; display: block;"
            :value="selectedKeys[0]"
            @change="(value) => setSelectedKeys(value ? [value] : [])"
            @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
          >
            <a-select-option v-for="(item, index) in PayTypeOpts" :value="index" :key="index">{{item}}</a-select-option>
          </a-select>
          <a-button
            type="primary"
            icon="search"
            size="small"
            style="width: 90px; margin-right: 8px"
            @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
          >
            搜索
          </a-button>
          <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
            重置
          </a-button>
        </div>
        <div
          slot="filterFee"
          slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
          style="padding: 8px"
        >
          <a-select
            :placeholder="`选择${column.title}`"
            style="width: 188px; margin-bottom: 8px; display: block;"
            :value="selectedKeys[0]"
            @change="(value) => setSelectedKeys(value ? [value] : [])"
            @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
          >
            <a-select-option v-for="(item, index) in IsTfOpts" :value="index" :key="index">{{item}}</a-select-option>
          </a-select>
          <a-button
            type="primary"
            icon="search"
            size="small"
            style="width: 90px; margin-right: 8px"
            @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
          >
            搜索
          </a-button>
          <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
            重置
          </a-button>
        </div>
        <div
          slot="filterStatus"
          slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
          style="padding: 8px"
        >
          <a-select
            :placeholder="`选择${column.title}`"
            style="width: 188px; margin-bottom: 8px; display: block;"
            :value="selectedKeys[0]"
            @change="(value) => setSelectedKeys(value ? [value] : [])"
            @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
          >
            <a-select-option v-for="(item, index) in statusOpts" :value="index" :key="index">{{item}}</a-select-option>
          </a-select>
          <a-button
            type="primary"
            icon="search"
            size="small"
            style="width: 90px; margin-right: 8px"
            @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
          >
            搜索
          </a-button>
          <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
            重置
          </a-button>
        </div>
        <template slot="budget_start" slot-scope="text, record">
          {{text}} ~ {{record.budget_end}}
        </template>
        <template slot="offer_start" slot-scope="text, record">
          {{text}} ~ {{record.offer_end}}
        </template>
        <template slot="username" slot-scope="text, record">
          <div><a-tag>{{text}}</a-tag></div>
          <div v-for="(item, index) in groupOpts" :key="index" style="margin: 5px 0">
            <div v-if="record.group_type == index"><a-tag>{{item}}组</a-tag></div>
          </div>
          <div><a-tag>{{record.leader_name}}</a-tag></div>
        </template>
        <template slot="category" slot-scope="text">第{{text}}类</template>
        <template slot="is_tf_fee" slot-scope="text">
          <div v-for="(item, index) in IsTfOpts" :key="index">
            <div v-if="text === index"><a-tag color="red">{{item}}</a-tag></div>
          </div>
        </template>
        <template slot="payment_type" slot-scope="text">
          <div v-for="(item, index) in PayTypeOpts" :key="index">
            <div v-if="text === index"><a-tag color="red">{{item}}</a-tag></div>
          </div>
        </template>
        <template slot="status" slot-scope="text">
          <a-tag color="red">{{statusOpts[text]}}</a-tag>
        </template>
        <template slot="action" slot-scope="text, record">
          <a-button size="small" type="primary" @click="toDetail(record.id)">查看</a-button>
        </template>
      </s-table>
    </a-card>
    <a-modal
      centered
      title="采购需求审核"
      v-model="visible"
      @ok="onSubmit"
    >
      <a-form-model ref="form" :model="form">
        <a-form-model-item label="需求审核">
          <a-radio-group v-model="form.status">
            <a-radio v-for="(item, index) in checkOpts" :value="index" :key="index">{{item}}</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item v-if="form.status == 22" label="审核不通过备注">
          <a-input type="textarea" v-model="form.notes" placeholder="注：如审核不通过请填写原因"/>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
  import { STable, Ellipsis } from '@/components'
  import transactionColumns from '@/columns/transaction'
  import { getPurchaseList, getCheckPurchase, postCheckPurchase } from '@/api/transaction'

  export default {
    name: 'Purchase',
    components: { STable, Ellipsis },
    data () {
      return {
        queryParam: {},
        IsTfOpts: {},
        PayTypeOpts: {},
        statusOpts: {},
        groupOpts: {},
        adviserOpts: {},
        categoryOpts: {},
        regionOpts: {},
        checkOpts: {},
        purchase_id: '',
        purchaseColumns: transactionColumns.purchase,
        visible: false,
        form: {},
        purchaseData: parameters => {
          return getPurchaseList(Object.assign(parameters, this.queryParam)).then(res => {
            if (res.status === 0) {
              this.IsTfOpts = res.result.IsTfOpts
              this.PayTypeOpts = res.result.PayTypeOpts
              this.statusOpts = res.result.statusOpts
              this.groupOpts = res.result.group_type
              this.adviserOpts = res.result.advisers
              this.categoryOpts = res.result.category_opts
              this.regionOpts = res.result.tm_region_opts
              return res.result
            }
          })
        }
      }
    },
    methods: {
      check (id) {
        const app = this
        getCheckPurchase(id).then(res => {
          if (res.status === 0) {
            app.purchase_id = id
            app.checkOpts = res.result.status_items
            app.visible = true
          }
        })
      },
      onSubmit () {
        const app = this
        const data = {}
        for (const k in app.form) {
          data['purchase[' + k + ']'] = app.form[k]
        }
        postCheckPurchase(app.purchase_id, data).then(res => {
          if (res.status === 0) {
            app.form = {}
            app.visible = false
            app.$refs.table.refresh(true)
          } else {
            app.$warning({
              title: res.msg
            })
          }
        })
      },
      toDetail (id) {
        this.$router.push({ name: 'PurchaseDetail', params: { id } })
      },
      handleSelect (selectedKeys, confirm, dataIndex) {
        confirm()
        this.searchText = selectedKeys[0]
        this.searchedColumn = dataIndex
        this.queryParam['filter[' + dataIndex + ']'] = selectedKeys[0]
      },
      handleSelect1 (selectedKeys, confirm, dataIndex) {
        confirm()
        this.searchText = selectedKeys[0]
        this.searchedColumn = dataIndex
        this.queryParam['filter[uid]'] = selectedKeys[0]
      },
      handleReset1 (clearFilters, dataIndex) {
        clearFilters()
        delete this.queryParam['filter[' + dataIndex + ']']
        delete this.queryParam['filter[uid]']
      },
      filterOption (input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        )
      },
      handleSearch (selectedKeys, confirm, dataIndex) {
        confirm()
        this.searchText = selectedKeys[0]
        this.searchedColumn = dataIndex
        this.queryParam['filter[' + dataIndex + ']'] = selectedKeys[0]
      },
      handleReset (clearFilters, dataIndex) {
        clearFilters()
        delete this.queryParam['filter[' + dataIndex + ']']
      }
    }
  }
</script>

<style scoped>

</style>
